package com.spring.demo.spring_boot.config;

import java.sql.SQLException;


import javax.sql.DataSource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@PropertySources(value = { @PropertySource(value = "classpath:application-dev-db.properties") })
public class DruidDataSourceConfig {
	private static Logger logger = LogManager.getLogger(DruidDataSourceConfig.class);
	@Autowired
	private Environment env;

	@Bean
	@Primary // 扫描其他数据源时，使用该数据源druid
	public DataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setName(env.getProperty("mysql.datasource.name").trim());
		dataSource.setUrl(env.getProperty("mysql.datasource.url").trim());
		dataSource.setUsername(env.getProperty("mysql.datasource.username").trim());
		dataSource.setPassword(env.getProperty("mysql.datasource.password").trim());
		dataSource.setDbType(env.getProperty("druid.datasource.type").trim());
		dataSource.setDriverClassName(env.getProperty("druid.datasource.driverClassName").trim());
		dataSource.setInitialSize(Integer.parseInt(env.getProperty("druid.datasource.initialSize").trim()));
		dataSource.setMinIdle(Integer.parseInt(env.getProperty("druid.datasource.minIdle").trim()));
		dataSource.setMaxActive(Integer.parseInt(env.getProperty("druid.datasource.maxActive").trim()));
		dataSource.setMaxWait(Long.parseLong(env.getProperty("druid.datasource.maxWait").trim()));
		dataSource.setTimeBetweenEvictionRunsMillis(
				Long.parseLong(env.getProperty("druid.datasource.timeBetweenEvictionRunsMillis").trim()));
		dataSource.setMinEvictableIdleTimeMillis(
				Long.parseLong(env.getProperty("druid.datasource.minEvictableIdleTimeMillis").trim()));
		dataSource.setValidationQuery(env.getProperty("druid.datasource.validationQuery").trim());
		dataSource.setTestWhileIdle(Boolean.parseBoolean(env.getProperty("druid.datasource.testWhileIdle").trim()));
		dataSource.setTestOnBorrow(Boolean.parseBoolean(env.getProperty("druid.datasource.testOnBorrow").trim()));
		dataSource.setTestOnReturn(Boolean.parseBoolean(env.getProperty("druid.datasource.testOnReturn").trim()));
		dataSource.setPoolPreparedStatements(
				Boolean.parseBoolean(env.getProperty("druid.datasource.poolPreparedStatements").trim()));
		dataSource.setMaxPoolPreparedStatementPerConnectionSize(
				Integer.parseInt(env.getProperty("druid.datasource.maxPoolPreparedStatementPerConnectionSize").trim()));
		try {
			dataSource.setFilters(env.getProperty("druid.datasource.filters").trim());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dataSource;
	}

	@Bean
	public DataSourceTransactionManager transactionManager() {
		logger.debug("> transactionManager");
		return new DataSourceTransactionManager(dataSource());
	}

}
